package so.sao.shop.supplier.service.app.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import so.sao.shop.supplier.config.Constant;
import so.sao.shop.supplier.dao.app.AppDeliveryAddressDao;
import so.sao.shop.supplier.domain.AppAddress;
import so.sao.shop.supplier.domain.DeliveryAddress;
import so.sao.shop.supplier.pojo.Result;
import so.sao.shop.supplier.pojo.input.AppDeliveryAddressInput;
import so.sao.shop.supplier.service.app.AppDeliveryAddressService;
import so.sao.shop.supplier.util.BeanMapper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 服务实现类
 * 
 * @author zhangruibing
 * @since 2017-07-17
 */
@Service
public class AppDeliveryAddressServiceImpl implements AppDeliveryAddressService {

	@Autowired
	private AppDeliveryAddressDao appDeliveryAddressDao;

	/**
	 * 新增收货地址
	 *
	 * @param appDeliveryAddressInput
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result saveDeliveryAddress(AppDeliveryAddressInput appDeliveryAddressInput) {

		if (appDeliveryAddressInput.getUserId()==null){
			return Result.fail("用户ID不能为空");
		}

		/*用户最大可添加10条地址数据*/
		if (appDeliveryAddressDao.countAddrNum(appDeliveryAddressInput.getUserId()) > Constant.DeliveryAddress.MAX_SAVE_DELIVERYADDRESS_NUM){
			return Result.fail("最多可添加"+Constant.DeliveryAddress.MAX_SAVE_DELIVERYADDRESS_NUM+"条地址信息");
		}

		/* 校验省市区编码匹配关系*/
		Result x = checkDeliveryAddress(appDeliveryAddressInput);
		if (x != null) {
			return x;
		}

		/*判断是否设置该地址被设为默认地址*/
		if (appDeliveryAddressInput.getAddrDefault() == 1){
			/*若被设为默认地址，先将其他所有地址设置为非默认*/
			appDeliveryAddressDao.updateDeliveryAddressStatus(appDeliveryAddressInput.getUserId());
		}

		DeliveryAddress deliveryAddress = BeanMapper.map(appDeliveryAddressInput, DeliveryAddress.class);
		deliveryAddress.setCreatedAt(new Date());
		deliveryAddress.setUpdatedAt(new Date());
		appDeliveryAddressDao.save(deliveryAddress);
		return Result.success("添加收货信息成功");
	}


	/**
	 * 编辑收货地址
	 *
	 * @param appDeliveryAddressInput
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result updateDeliveryAddress(AppDeliveryAddressInput appDeliveryAddressInput) {

		if (appDeliveryAddressInput.getAddrId()==null){
			return Result.fail("收货地址ID不能为空");
		}

		/* 校验省市区编码匹配及省市区关联关系*/
		Result x = checkDeliveryAddress(appDeliveryAddressInput);
		if (x != null) {
			return x;
		}

		/*判断是否设置该地址被设为默认地址*/
		if (appDeliveryAddressInput.getAddrDefault() == 1){
			/*若被设为默认地址，先将其他所有地址设置为非默认*/
			appDeliveryAddressDao.updateDeliveryAddressStatus(appDeliveryAddressInput.getUserId());
		}

		DeliveryAddress deliveryAddress = BeanMapper.map(appDeliveryAddressInput, DeliveryAddress.class);
		deliveryAddress.setUpdatedAt(new Date());
		appDeliveryAddressDao.update(deliveryAddress);
		return Result.success("更新收货信息成功");
	}

	/**
	 * 批量删除收货地址
	 *
	 * @param addrIds
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result deleteDeliveryAddress(String addrIds, String userId) {

		String[] addrIdList =addrIds.split(",");
		List<Long> addrIdList1 = new ArrayList<>();
		for (String addrId : addrIdList){
			addrIdList1.add(Long.parseLong(addrId));
		}

		appDeliveryAddressDao.deleteByAddrIds(addrIdList1);
		return Result.success("删除收货信息成功");
	}

	/**
	 * 根据用户id查询收货地址列表
	 *
	 * @param userIds
	 * @return
	 */
	@Override
	public Result findByUserId(String userIds) {

		List<DeliveryAddress> list = appDeliveryAddressDao.findByUserId(Long.parseLong(userIds));
		if (list == null || list.size() == 0)
		{
			return Result.success("查询成功，查无数据");
		}else{
			return Result.success("查询成功", list);
		}
	}


	/**
	 * 更改默认的收货地址
	 *
	 * @param addrId
	 * @param userId
	 * @return
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Result updateDeliveryAddressStatus(String addrId, String userId) {

		// step 1 状态归零
		appDeliveryAddressDao.updateDeliveryAddressStatus(Long.parseLong(userId));
		// step 2 设为默认
		appDeliveryAddressDao.updateDeliveryAddressStatusByid(Long.parseLong(addrId));

		return Result.success("设置默认收货地址成功");
	}

	/**
	 * <p>根据用户ID查询默认地址</p>
	 *
	 * @param userId
	 * @return
	 */
	@Override
	public Result findDefaultDeliveryAddrByUserId(String userId) {
		DeliveryAddress deliveryAddress = appDeliveryAddressDao.findDefaultDeliveryAddrByUserId(Long.parseLong(userId));
		if (deliveryAddress != null){
			return Result.success("获取默认地址成功",deliveryAddress);
		}else {
			return Result.success("查询成功，查无数据");
		}
	}

	/**
	 * 获取所有省市区信息
	 *
	 * @return
	 */
	@Override
	public List<AppAddress> getAllRegion() {
		return appDeliveryAddressDao.findAllAddress();
	}

	/**
	 * 校验省市区编码匹配关系
	 * @param appDeliveryAddressInput
	 * @return
	 */
	private Result checkDeliveryAddress(AppDeliveryAddressInput appDeliveryAddressInput) {
		/*省市区编码串*/
		String codeString = appDeliveryAddressInput.getAddrProvinceCode()+appDeliveryAddressInput.getAddrCityCode()+appDeliveryAddressInput.getAddrAreaCode();
	   /*省市区编码匹配校验*/
		if (!(codeString.equals(appDeliveryAddressDao.findCodeStringByAreaCode(appDeliveryAddressInput.getAddrAreaCode())))){
			return Result.fail("省/市/区编码不匹配");
		}
		return null;
	}

}